<?php

namespace app\reporting\logic;

use app\common\library\Excel;
use think\facade\Db;

class ReportVisitLogic extends BasicLogic
{

    /**
     * @desc   获取报告信息
     * @author luckyXiong
     * @date   2024-11-05
     */
    public function getList($params=[]){
        $limit                = getIsset($params,'limit',10);
        $shop_id              = getIsset($params,'shop_id');
        $create_time          = getIsset($params,'create_time');
        $adminId              = $params['adminId']??'';
        $groupId              = $params['groupId']??'';
        $total_visitors       = $params['total_visitors']??'';
        $total_sessions       = $params['total_sessions']??'';
        $total_carts          = $params['total_carts']??'';
        $total_checkouts      = $params['total_checkouts']??'';
        $avg_duration         = $params['avg_duration']??'';
        $total_bounce_rate    = $params['total_bounce_rate']??'';
        $total_conversion     = $params['total_conversion']??'';
        $total_orders_placed  = $params['total_orders_placed']??'';
        $total_pageviews      = $params['total_pageviews']??'';
        $page_type            = $params['page_type']??'';
        $spu                  = $params['spu']??'';
        $admin_id             = $params['admin_id']??'';
        $where                = $order = [];

        if ($create_time) {
            $where[]  = ['create_time', '>=', date('Y-m-d H:i:s',strtotime($create_time[0]))];
            $where[]  = ['create_time', '<=', date('Y-m-d H:i:s',strtotime($create_time[1]) + 3600*24-1)];
        }

        if($shop_id){
            $where[] = ['shop_id','=',$shop_id];
        }

        if($page_type){
            $where[] = ['page_type','=',$page_type];
        }

        if($spu){
            $where[] = ['spu','=',$spu];
        }

        if($admin_id){
            $where[] = ['admin_id', 'in',$admin_id];
        }

        if($total_visitors){
            $order['total_visitors'] = $total_visitors;
        }

        if($total_sessions){
            $order['total_sessions'] = $total_sessions;
        }

        if($total_carts){
            $order['total_carts'] = $total_carts;
        }


        if($total_checkouts){
            $order['total_checkouts'] = $total_checkouts;
        }

        if($avg_duration){
            $order['avg_duration'] = $avg_duration;
        }

        if($total_bounce_rate){
            $order['total_bounce_rate'] = $total_bounce_rate;
        }

        if($total_conversion){
            $order['total_conversion'] = $total_conversion;
        }

        if($total_orders_placed){
            $order['total_orders_placed'] = $total_orders_placed;
        }

        if($total_pageviews){
            $order['total_pageviews'] = $total_pageviews;
        }

        $shop_admin = Config('Logisticskey.shop_admin');

        if(!$total_visitors && !$total_sessions &&!$total_carts &&!$total_checkouts && $avg_duration && $total_bounce_rate && $total_conversion &&$total_orders_placed && $total_pageviews){
            $order['create_time'] = 'desc';
        }

        $shop_admin_view = Config('Logisticskey.shop_admin_view');

        //2025-04-12  运营查看自己账号;
        if((in_array($groupId,[11])) && !in_array($adminId,$shop_admin)){
            $where[] = ['admin_id', 'like', '%' . $adminId . '%'];
        }


        if(in_array($adminId,$shop_admin)){ //店长
            $where[] = ['shop_id', '=', $shop_admin_view[$adminId]];
        }

        $field = '*';
        $res   = Db::name('order_page_visits')
            ->field($field)
            ->where($where)
            ->order($order)
            ->paginate($limit);
        $list = $res->items()?:[];

        if($list){
            //批量获取店铺名称;
            $shop_ids       = array_column($list,'shop_id');
            $shop_names     = Db::name('shops')->whereIn('id',$shop_ids)->select()->toArray();
            $shop_names_map = array_column($shop_names,null,'id');
            foreach($list as $k=>$v){
                //获取运营维护信息;
                $admin_info = Db::name('order_page_visits_admin')->where(['shop_id'=>$v['shop_id'],'page_path'=>$v['page_path']])->find();
                //获取产品信息
                if($v['page_type']=='Product'){
                    $spu    = Db::name('order_page_visits_product')->where(['shop_id'=>$v['shop_id'],'page_path'=>$v['page_path']])->value('spu');
                }else{
                    $spu    = $admin_info['spu']??'';
                }
                $shopNames             = $shop_names_map[$v['shop_id']]??[];

                $list[$k]['shop_name'] = $shopNames['name']??'';
                $domain                = $shopNames['domain']??'';
                if(!empty($v['avg_duration'])){
                    $list[$k]['avg_duration'] = convertToTimeFormat($v['avg_duration']);
                }
                $user_id               = $admin_info['admin_id']??'';
                $adminNames            = self::get_admin_name($user_id);
                $list[$k]['admin_name']= $adminNames;
                $list[$k]['spu']= $spu;

                if(!empty($v['total_bounce_rate'])){
                    $total_bounce_rate             = getResFour($v['total_bounce_rate']);
                    $list[$k]['total_bounce_rate'] = ($total_bounce_rate*100).'%';
                }else{
                    $list[$k]['total_bounce_rate'] = '0.00%';
                }

                if(!empty($v['total_conversion'])){
                    $total_conversion             = getResFour($v['total_conversion']);
                    $list[$k]['total_conversion'] = ($total_conversion*100).'%';
                }else{
                    $list[$k]['total_conversion'] = '0.00%';
                }

                if($v['page_path']){
                    $list[$k]['product_url'] = 'https://'.$domain.$v['page_path'];
                }else{
                    $list[$k]['product_url'] = '';
                }
            }
        }


        $data = [
            'list'   => $list,
            'total'  => $res->total(),
        ];
        return  $this->jsonMessage(200,true,'获取成功',$data);
    }



    /**
     * @desc    获取物流状态信息
     * @date    2024/12/29
     * @author  zhangheng
     */
    public function get_page_type(){
        $size_type   = Config('Logisticskey.page_type');
        $info        = getLevel($size_type);
        foreach($info as $v){
            $arr[] = [
                'value'      => $v['key'],
                'label'      => $v['value'],
            ];
        }
        return  $this->jsonMessage(200,true,'获取成功!',$arr);
    }


    /**
     * @desc   导出报告信息
     * @author luckyXiong
     * @date   2024-11-05
     */
    public function exportReport($params=[]){
        $list     = self::get_export_info($params);
        $header[] = ['店铺', 'shop_name'];
        $header[] = ['页面类型', 'page_type'];
        $header[] = ['页面路径', 'page_path'];
        $header[] = ['访客','total_visitors'];
        $header[] = ['访问', 'total_sessions'];
        $header[] = ['添加到购物车', 'total_carts'];
        $header[] = ['进入结算流程', 'total_checkouts'];
        $header[] = ['访问持续时间', 'avg_duration'];
        $header[] = ['跳出率', 'total_bounce_rate'];
        $header[] = ['转化率', 'total_conversion'];
        $header[] = ['成功准换的访问数', 'total_orders_placed'];
        $header[] = ['页面浏览量', 'total_pageviews'];
        $header[] = ['运营', 'admin_name'];
        $header[] = ['创建时间', 'create_time'];
        $fileName = "Report_day_visits";
        return Excel::exportData($list, $header, $fileName, 'xlsx');
    }



    /**
     * @desc   导出页面访问情况
     * @author luckyXiong
     * @date   2024-11-05
     */
    public function exportPathReport($params=[]){
        $list     = self::get_export_path($params);
        $header[] = ['店铺', 'shop_name'];
        $header[] = ['页面类型', 'page_type'];
        $header[] = ['页面路径', 'page_path'];
        $header[] = ['在线商店访客数','total_visitors'];
        $header[] = ['访问量', 'total_sessions'];
        $header[] = ['加购访问次数', 'total_carts'];
        $header[] = ['到达结账页面的访问次数', 'total_checkouts'];
        $header[] = ['创建时间', 'create_time'];
        $fileName = "Report_day_path";
        return Excel::exportData($list, $header, $fileName, 'xlsx');
    }


    /**
     * @desc   产品数据导入
     * @author luckyXiong
     * @date   2024-11-05
     */
    public function get_export_info($params=[]){
        $shop_id              = getIsset($params,'shop_id');
        $create_time          = getIsset($params,'create_time');
        $total_visitors       = $params['total_visitors']??'';
        $total_sessions       = $params['total_sessions']??'';
        $total_carts          = $params['total_carts']??'';
        $total_checkouts      = $params['total_checkouts']??'';
        $avg_duration         = $params['avg_duration']??'';
        $total_bounce_rate    = $params['total_bounce_rate']??'';
        $total_conversion     = $params['total_conversion']??'';
        $total_orders_placed  = $params['total_orders_placed']??'';
        $total_pageviews      = $params['total_pageviews']??'';
        $where                = $order = [];

        if ($create_time) {
            $where[]  = ['create_time', '>=', date('Y-m-d H:i:s',strtotime($create_time[0]))];
            $where[]  = ['create_time', '<=', date('Y-m-d H:i:s',strtotime($create_time[1]) + 3600*24-1)];
        }

        if($shop_id){
            $where[] = ['shop_id','=',$shop_id];
        }

        if($total_visitors){
            $order['total_visitors'] = $total_visitors;
        }

        if($total_sessions){
            $order['total_sessions'] = $total_sessions;
        }

        if($total_carts){
            $order['total_carts'] = $total_carts;
        }


        if($total_checkouts){
            $order['total_checkouts'] = $total_checkouts;
        }

        if($avg_duration){
            $order['avg_duration'] = $avg_duration;
        }

        if($total_bounce_rate){
            $order['total_bounce_rate'] = $total_bounce_rate;
        }

        if($total_conversion){
            $order['total_conversion'] = $total_conversion;
        }

        if($total_orders_placed){
            $order['total_orders_placed'] = $total_orders_placed;
        }

        if($total_pageviews){
            $order['total_pageviews'] = $total_pageviews;
        }



        if(!$total_visitors && !$total_sessions &&!$total_carts &&!$total_checkouts && $avg_duration && $total_bounce_rate && $total_conversion &&$total_orders_placed && $total_pageviews){
            $order['create_time'] = 'desc';
        }

        $field = '*';
        $list   = Db::name('order_page_visits')
            ->field($field)
            ->where($where)
            ->order($order)
            ->select();
        $list = objectToArray($list);

        if($list){
            //批量获取店铺名称;
            $shop_ids       = array_column($list,'shop_id');
            $shop_names     = Db::name('shops')->whereIn('id',$shop_ids)->select()->toArray();
            $shop_names_map = array_column($shop_names,null,'id');
            foreach($list as $k=>$v){
                $shopNames             = $shop_names_map[$v['shop_id']]??[];
                $list[$k]['shop_name'] = $shopNames['name']??'';
                if(!empty($v['avg_duration'])){
                    $list[$k]['avg_duration'] = convertToTimeFormat($v['avg_duration']);
                }

                if(!empty($v['total_bounce_rate'])){
                    $total_bounce_rate             = getResFour($v['total_bounce_rate']);
                    $list[$k]['total_bounce_rate'] = ($total_bounce_rate*100).'%';
                }else{
                    $list[$k]['total_bounce_rate'] = '0.00%';
                }

                if(!empty($v['total_conversion'])){
                    $total_conversion             = getResFour($v['total_conversion']);
                    $list[$k]['total_conversion'] = ($total_conversion*100).'%';
                }else{
                    $list[$k]['total_conversion'] = '0.00%';
                }
                $list[$k]['admin_name']= '';
            }
        }
        return $list;
    }


    /**
     * @desc   产品数据导入
     * @author luckyXiong
     * @date   2025-03-13
     */
    public function get_export_path($params=[]){
        $shop_id              = getIsset($params,'shop_id');
        $create_time          = getIsset($params,'create_time');
        $total_visitors       = $params['total_visitors']??'';
        $total_sessions       = $params['total_sessions']??'';
        $total_carts          = $params['total_carts']??'';
        $total_checkouts      = $params['total_checkouts']??'';
        $where                = $order = [];

        if ($create_time) {
            $where[]  = ['create_time', '>=', date('Y-m-d H:i:s',strtotime($create_time[0]))];
            $where[]  = ['create_time', '<=', date('Y-m-d H:i:s',strtotime($create_time[1]) + 3600*24-1)];
        }

        if($shop_id){
            $where[] = ['shop_id','=',$shop_id];
        }

        if($total_visitors){
            $order['total_visitors'] = $total_visitors;
        }

        if($total_sessions){
            $order['total_sessions'] = $total_sessions;
        }

        if($total_carts){
            $order['total_carts'] = $total_carts;
        }


        if($total_checkouts){
            $order['total_checkouts'] = $total_checkouts;
        }


        if(!$total_visitors && !$total_sessions &&!$total_carts &&!$total_checkouts ){
            $order['create_time'] = 'desc';
        }

        $field = '*';
        $list   = Db::name('order_path_visits')
            ->field($field)
            ->where($where)
            ->order($order)
            ->select()
            ->toArray();
        if($list){
            //批量获取店铺名称;
            $shop_ids       = array_column($list,'shop_id');
            $shop_names     = Db::name('shops')->whereIn('id',$shop_ids)->select()->toArray();
            $shop_names_map = array_column($shop_names,null,'id');
            foreach($list as $k=>$v){
                $shopNames             = $shop_names_map[$v['shop_id']]??[];
                $list[$k]['shop_name'] = $shopNames['name']??'';
            }
        }
        return $list;
    }




    /**
     * @desc   产品数据导入
     * @author luckyXiong
     * @date   2024-11-06
     */
    public function get_export_history($params=[]){
        $date                 = $params['date'] ??'';
        $customDate           = $params['customDate'] ?? 0;
        $groupId              = $params['groupId'] ?? '';
        $adminId              = $params['adminId'] ?? '';
        $admin_id             = $params['admin_id'] ?? '';
        $total_visitors       = $params['total_visitors']??'';
        $total_sessions       = $params['total_sessions']??'';
        $total_carts          = $params['total_carts']??'';
        $total_checkouts      = $params['total_checkouts']??'';
        $avg_duration         = $params['avg_duration']??'';
        $total_bounce_rate    = $params['total_bounce_rate']??'';
        $total_conversion     = $params['total_conversion']??'';
        $total_orders_placed  = $params['total_orders_placed']??'';
        $total_pageviews      = $params['total_pageviews']??'';
        $where                = $order = [];

        if($total_visitors){
            $order['total_visitors'] = $total_visitors;
        }

        if($total_sessions){
            $order['total_sessions'] = $total_sessions;
        }

        if($total_carts){
            $order['total_carts'] = $total_carts;
        }


        if($total_checkouts){
            $order['total_checkouts'] = $total_checkouts;
        }

        if($avg_duration){
            $order['avg_duration'] = $avg_duration;
        }

        if($total_bounce_rate){
            $order['total_bounce_rate'] = $total_bounce_rate;
        }

        if($total_conversion){
            $order['total_conversion'] = $total_conversion;
        }

        if($total_orders_placed){
            $order['total_orders_placed'] = $total_orders_placed;
        }

        if($total_pageviews){
            $order['total_pageviews'] = $total_pageviews;
        }



        if(!$total_visitors && !$total_sessions &&!$total_carts &&!$total_checkouts && $avg_duration && $total_bounce_rate && $total_conversion &&$total_orders_placed && $total_pageviews){
            $order['create_time'] = 'desc';
        }


        if($customDate){
            switch ($customDate) {
                case '1':
                    //实时
                    $yesterday = date('Y-m-d', time() - timezone());
                    $midnightYesterday =  date("Y-m-d H:i:s", strtotime($yesterday . ' 00:00:00'));
                    $endOfYesterday    =  date("Y-m-d H:i:s", strtotime($yesterday . ' 23:59:59'));
                    $where[]           =  ['create_time','between', [$midnightYesterday, $endOfYesterday]];
                    break;
                case '2':
                    $yesterday = date('Y-m-d', strtotime('-1 day') - timezone());
                    $midnightYesterday = date("Y-m-d H:i:s", strtotime($yesterday . ' 00:00:00'));
                    $endOfYesterday    = date("Y-m-d H:i:s", strtotime($yesterday . ' 23:59:59'));
                    $where[]           = ['create_time','between', [$midnightYesterday, $endOfYesterday]];
                    break;
                case '3':
                    $yesterday    =  date("Y-m-d H:i:s", strtotime(date('Y-m-d', time() - timezone())));
                    $yesterdayEnd =  date("Y-m-d H:i:s", strtotime(date('Y-m-d', strtotime('-6 day') - timezone())));


                    $where[]      = ['create_time','between', [$yesterday, $yesterdayEnd]];
                    break;
                default:
                    $yesterday = date('Y-m-d', time() - timezone());
                    $midnightYesterday = $yesterday . ' 00:00:00';
                    $endOfYesterday    = $yesterday . ' 23:59:59';
                    $where[]           = ['create_time','between', [$midnightYesterday, $endOfYesterday]];
            }
        }else{
            if(!empty($date)) {
                $midnightYesterday = $date['0']. ' 00:00:00';
                $endOfYesterday    = date("Y-m-d H:i:s", strtotime($date['1']) + 3600*24-1);
                $where[]           = ['create_time','between', [$midnightYesterday, $endOfYesterday]];
            }else{
                $yesterday = date('Y-m-d', time() - timezone());
                $midnightYesterday = $yesterday . ' 00:00:00';
                $endOfYesterday    = $yesterday . ' 23:59:59';
                $where[]           = ['create_time','between', [$midnightYesterday, $endOfYesterday]];
            }
        }

        if ($admin_id) {
            $where[] = ['admin_id', 'like', '%' . $admin_id . '%'];
        }


        //2024-11-06  运营产看自己账号;
        if(in_array($groupId,[11,17,20,19])){
            $where[] = ['admin_id', 'like', '%' . $adminId . '%'];
        }


        $field      = '*';
        $list       = Db::name('order_page_visits_history')
            ->field($field)
            ->where($where)
            ->order($order)
            ->select();
        $list = objectToArray($list);


        if($list){
            //批量获取店铺名称;
            $shop_ids       = array_column($list,'shop_id');
            $shop_names     = Db::name('shops')->field('id,name')->whereIn('id',$shop_ids)->select()->toArray();
            $shop_names_map = array_column($shop_names,null,'id');
            foreach($list as $k=>$v){
                $shopNames             = $shop_names_map[$v['shop_id']]??[];
                $list[$k]['shop_name'] = $shopNames['name']??'';
                $domain                = $shopNames['domain']??'';
                $user_id = $v['admin_id'];
                if(in_array($groupId,[11,17,20,19])){
                    $user_id = $adminId;
                }
                $list[$k]['admin_name']= self::get_admin_name($user_id);

                if(!empty($v['avg_duration'])){
                    $list[$k]['avg_duration'] = convertToTimeFormat($v['avg_duration']);
                }

                if(!empty($v['total_bounce_rate'])){
                    $total_bounce_rate             = getResFour($v['total_bounce_rate']);
                    $list[$k]['total_bounce_rate'] = ($total_bounce_rate*100).'%';
                }else{
                    $list[$k]['total_bounce_rate'] = '0.00%';
                }

                if(!empty($v['total_conversion'])){
                    $total_conversion             = getResFour($v['total_conversion']);
                    $list[$k]['total_conversion'] = ($total_conversion*100).'%';
                }else{
                    $list[$k]['total_conversion'] = '0.00%';
                }

                if($v['page_path']){
                    $list[$k]['product_url'] = 'https://'.$domain.$v['page_path'];
                }else{
                    $list[$k]['product_url'] = '';
                }
            }
        }
        return  $list;
    }





    /**
     * @desc   历史数据列表
     * @author luckyXiong
     * @date   2024-11-06
     */
    public function getHistoryList($params=[]){
        $limit                = $params['limit'] ?? 10;
        $date                 = $params['date'] ??'';
        $customDate           = $params['customDate'] ?? 0;
        $groupId              = $params['groupId'] ?? '';
        $adminId              = $params['adminId'] ?? '';
        $admin_id             = $params['admin_id'] ?? '';
        $shop_id              = $params['shop_id'] ?? '';
        $total_visitors       = $params['total_visitors']??'';
        $total_sessions       = $params['total_sessions']??'';
        $total_carts          = $params['total_carts']??'';
        $total_checkouts      = $params['total_checkouts']??'';
        $avg_duration         = $params['avg_duration']??'';
        $total_bounce_rate    = $params['total_bounce_rate']??'';
        $total_conversion     = $params['total_conversion']??'';
        $total_orders_placed  = $params['total_orders_placed']??'';
        $total_pageviews      = $params['total_pageviews']??'';
        $where                =  $order = [];

        if($total_visitors){
            $order['total_visitors'] = $total_visitors;
        }

        if($total_sessions){
            $order['total_sessions'] = $total_sessions;
        }

        if($total_carts){
            $order['total_carts'] = $total_carts;
        }


        if($total_checkouts){
            $order['total_checkouts'] = $total_checkouts;
        }

        if($avg_duration){
            $order['avg_duration'] = $avg_duration;
        }

        if($total_bounce_rate){
            $order['total_bounce_rate'] = $total_bounce_rate;
        }

        if($total_conversion){
            $order['total_conversion'] = $total_conversion;
        }

        if($total_orders_placed){
            $order['total_orders_placed'] = $total_orders_placed;
        }

        if($total_pageviews){
            $order['total_pageviews'] = $total_pageviews;
        }


        $shop_admin = Config('Logisticskey.shop_admin');

        $shop_admin_view = Config('Logisticskey.shop_admin_view');

        //2025-04-12  运营查看自己账号;
        if((in_array($groupId,[11])) && !in_array($adminId,$shop_admin)){
            $where[] = ['admin_id', 'like', '%' . $adminId . '%'];
        }




        if($shop_id && !in_array($adminId,$shop_admin)){
            $where[] = ['shop_id', '=', $shop_id];
        }


        if(in_array($adminId,$shop_admin)){ //店长
            $where[] = ['shop_id', '=', $shop_admin_view[$adminId]];
        }


        if(!$total_visitors && !$total_sessions &&!$total_carts &&!$total_checkouts && $avg_duration && $total_bounce_rate && $total_conversion &&$total_orders_placed && $total_pageviews){
            $order['create_time'] = 'desc';
        }


        if($customDate){
            switch ($customDate) {
                case '1':
                    //实时
                    $yesterday = date('Y-m-d', time() - timezone());
                    $midnightYesterday =  date("Y-m-d H:i:s", strtotime($yesterday . ' 00:00:00'));
                    $endOfYesterday    =  date("Y-m-d H:i:s", strtotime($yesterday . ' 23:59:59'));
                    $where[]           =  ['create_time','between', [$midnightYesterday, $endOfYesterday]];
                    break;
                case '2':
                    $yesterday = date('Y-m-d', strtotime('-1 day') - timezone());
                    $midnightYesterday = date("Y-m-d H:i:s", strtotime($yesterday . ' 00:00:00'));
                    $endOfYesterday    = date("Y-m-d H:i:s", strtotime($yesterday . ' 23:59:59'));
                    $where[]           = ['create_time','between', [$midnightYesterday, $endOfYesterday]];
                    break;
                case '3':
                    $yesterday    =  date("Y-m-d H:i:s", strtotime(date('Y-m-d', strtotime('-6 day') - timezone())));
                    $yesterdayEnd =  date("Y-m-d H:i:s", strtotime(date('Y-m-d', time() - timezone())));
                    $where[]      = ['create_time','between', [$yesterday, $yesterdayEnd]];
                    break;
                default:
                    $yesterday = date('Y-m-d', time() - timezone());
                    $midnightYesterday = $yesterday . ' 00:00:00';
                    $endOfYesterday    = $yesterday . ' 23:59:59';
                    $where[]           = ['create_time','between', [$midnightYesterday, $endOfYesterday]];
            }
        }else{
            if(!empty($date)) {
                $midnightYesterday = $date['0']. ' 00:00:00';
                $endOfYesterday    = date("Y-m-d H:i:s", strtotime($date['1']) + 3600*24-1);
                $where[]           = ['create_time','between', [$midnightYesterday, $endOfYesterday]];
            }else{
                $yesterday = date('Y-m-d', time() - timezone());
                $midnightYesterday = $yesterday . ' 00:00:00';
                $endOfYesterday    = $yesterday . ' 23:59:59';
                $where[]           = ['create_time','between', [$midnightYesterday, $endOfYesterday]];
            }
        }

        if ($admin_id) {
            $where[] = ['admin_id', 'like', '%' . $admin_id . '%'];
        }

        //2024-11-06  运营查看自己账号;
        if(in_array($groupId,[11,17,20,19])){
            $where[] = ['admin_id', 'like', '%' . $adminId . '%'];
        }


        $field = '*';
        $res   = Db::name('order_page_visits_history')
            ->field($field)
            ->where($where)
            ->order($order)
            ->paginate($limit);
        $list = $res->items()?:[];

        if($list){
            //批量获取店铺名称;
            $shop_ids       = array_column($list,'shop_id');
            $shop_names     = Db::name('shops')->field('id,domain,name')->whereIn('id',$shop_ids)->select()->toArray();
            $shop_names_map = array_column($shop_names,null,'id');


            foreach($list as $k=>$v){
                $shopNames             = $shop_names_map[$v['shop_id']]??[];
                $list[$k]['shop_name'] = $shopNames['name']??'';
                $domain                =  $shopNames['domain'] ?? '';
                $user_id = $v['admin_id'];
                if(in_array($groupId,[11,17,20,19])){
                    $user_id = $adminId;
                }
                $adminNames            = self::get_admin_name($user_id);
                $list[$k]['admin_name']= $adminNames;
                $list[$k]['admin_id']  = $v['admin_id'] ? explode(',',$v['admin_id']) : '';

                if(!empty($v['avg_duration'])){
                    $list[$k]['avg_duration'] = convertToTimeFormat($v['avg_duration']);
                }

                if(!empty($v['total_bounce_rate'])){
                    $total_bounce_rate             = getResFour($v['total_bounce_rate']);
                    $list[$k]['total_bounce_rate'] = ($total_bounce_rate*100).'%';
                }else{
                    $list[$k]['total_bounce_rate'] = '0.00%';
                }

                if(!empty($v['total_conversion'])){
                    $total_conversion             = getResFour($v['total_conversion']);
                    $list[$k]['total_conversion'] = ($total_conversion*100).'%';
                }else{
                    $list[$k]['total_conversion'] = '0.00%';
                }
                if($v['page_path']){
                    $list[$k]['product_url'] = 'https://'.$domain.$v['page_path'];
                }else{
                    $list[$k]['product_url'] =  '';
                }
            }
        }

        $data = [
            'list'   => $list,
            'total'  => $res->total(),
        ];
        return  $this->jsonMessage(200,true,'获取成功',$data);

    }


    /**
     * @desc   历史数据列表
     * @author luckyXiong
     * @date   2024-11-06
     */
    public function getHistoryPerson($params=[]){
        $limit                = $params['limit'] ?? 10;
        $date                 = $params['date'] ??'';
        $spu                  = $params['spu'] ??'';
        $customDate           = $params['customDate'] ?? 0;
        $adminId              = $params['adminId'] ?? '';
        $page_type            = $params['page_type'] ?? '';
        $total_visitors       = $params['total_visitors']??'';
        $total_sessions       = $params['total_sessions']??'';
        $total_carts          = $params['total_carts']??'';
        $total_checkouts      = $params['total_checkouts']??'';
        $avg_duration         = $params['avg_duration']??'';
        $total_bounce_rate    = $params['total_bounce_rate']??'';
        $total_conversion     = $params['total_conversion']??'';
        $total_orders_placed  = $params['total_orders_placed']??'';
        $total_pageviews      = $params['total_pageviews']??'';
        $where                = $order = [];

        if($total_visitors){
            $order['total_visitors'] = $total_visitors;
        }

        if($total_sessions){
            $order['total_sessions'] = $total_sessions;
        }

        if($page_type){
            $order['page_type'] = $page_type;
        }

        if($spu){
            $order['spu'] = $spu;
        }

        if($total_carts){
            $order['total_carts'] = $total_carts;
        }


        if($total_checkouts){
            $order['total_checkouts'] = $total_checkouts;
        }

        if($avg_duration){
            $order['avg_duration'] = $avg_duration;
        }

        if($total_bounce_rate){
            $order['total_bounce_rate'] = $total_bounce_rate;
        }

        if($total_conversion){
            $order['total_conversion'] = $total_conversion;
        }

        if($total_orders_placed){
            $order['total_orders_placed'] = $total_orders_placed;
        }

        if($total_pageviews){
            $order['total_pageviews'] = $total_pageviews;
        }




        if(!$total_visitors && !$total_sessions &&!$total_carts &&!$total_checkouts && $avg_duration && $total_bounce_rate && $total_conversion &&$total_orders_placed && $total_pageviews){
            $order['create_time'] = 'desc';
        }


        if($customDate){
            switch ($customDate) {
                case '1':
                    //实时
                    $yesterday = date('Y-m-d', time() - timezone());
                    $midnightYesterday =  date("Y-m-d H:i:s", strtotime($yesterday . ' 00:00:00'));
                    $endOfYesterday    =  date("Y-m-d H:i:s", strtotime($yesterday . ' 23:59:59'));
                    $where[]           =  ['create_time','between', [$midnightYesterday, $endOfYesterday]];
                    break;
                case '2':
                    $yesterday = date('Y-m-d', strtotime('-1 day') - timezone());
                    $midnightYesterday = date("Y-m-d H:i:s", strtotime($yesterday . ' 00:00:00'));
                    $endOfYesterday    = date("Y-m-d H:i:s", strtotime($yesterday . ' 23:59:59'));
                    $where[]           = ['create_time','between', [$midnightYesterday, $endOfYesterday]];
                    break;
                case '3':
                    $yesterday    =  date("Y-m-d H:i:s", strtotime(date('Y-m-d', strtotime('-6 day') - timezone())));
                    $yesterdayEnd =  date("Y-m-d H:i:s", strtotime(date('Y-m-d', time() - timezone())));
                    $where[]      = ['create_time','between', [$yesterday, $yesterdayEnd]];
                    break;
                default:
                    $yesterday = date('Y-m-d', time() - timezone());
                    $midnightYesterday = $yesterday . ' 00:00:00';
                    $endOfYesterday    = $yesterday . ' 23:59:59';
                    $where[]           = ['create_time','between', [$midnightYesterday, $endOfYesterday]];
            }
        }else{
            if(!empty($date)) {
                $midnightYesterday = $date['0']. ' 00:00:00';
                $endOfYesterday    = date("Y-m-d H:i:s", strtotime($date['1']) + 3600*24-1);
                $where[]           = ['create_time','between', [$midnightYesterday, $endOfYesterday]];
            }else{
                $yesterday = date('Y-m-d', time() - timezone());
                $midnightYesterday = $yesterday . ' 00:00:00';
                $endOfYesterday    = $yesterday . ' 23:59:59';
                $where[]           = ['create_time','between', [$midnightYesterday, $endOfYesterday]];
            }
        }

        $where[] = ['admin_id', '=', $adminId];

        $field = '*';
        $res   = Db::name('order_page_visits_history')
            ->field($field)
            ->where($where)
            ->order($order)
            ->paginate($limit);
        $list = $res->items()?:[];

        if($list){
            //批量获取店铺名称;
            $shop_ids       = array_column($list,'shop_id');
            $shop_names     = Db::name('shops')->field('id,domain,name')->whereIn('id',$shop_ids)->select()->toArray();
            $shop_names_map = array_column($shop_names,null,'id');
            foreach($list as $k=>$v){
                $shopNames             = $shop_names_map[$v['shop_id']]??[];
                $list[$k]['shop_name'] = $shopNames['name']??'';
                $user_id = $v['admin_id'];
                $domain                =  $shopNames['domain'] ?? '';
                $adminNames            = self::get_admin_name($user_id);
                $list[$k]['admin_name']= $adminNames;
                $list[$k]['admin_id']  = $v['admin_id'] ? explode(',',$v['admin_id']) : '';

                if(!empty($v['avg_duration'])){
                    $list[$k]['avg_duration'] = convertToTimeFormat($v['avg_duration']);
                }

                if(!empty($v['total_bounce_rate'])){
                    $total_bounce_rate             = getResFour($v['total_bounce_rate']);
                    $list[$k]['total_bounce_rate'] = ($total_bounce_rate*100).'%';
                }else{
                    $list[$k]['total_bounce_rate'] = '0.00%';
                }

                if(!empty($v['total_conversion'])){
                    $total_conversion             = getResFour($v['total_conversion']);
                    $list[$k]['total_conversion'] = ($total_conversion*100).'%';
                }else{
                    $list[$k]['total_conversion'] = '0.00%';
                }

                if($v['page_path']){
                    $list[$k]['product_url'] = 'https://'.$domain.$v['page_path'];
                }else{
                    $list[$k]['product_url'] =  '';
                }
            }
        }

        $data = [
            'list'   => $list,
            'total'  => $res->total(),
        ];
        return  $this->jsonMessage(200,true,'获取成功',$data);
    }


    /**
     * @desc   运营广告花费列表
     * @author luckyXiong
     * @date   2025-04-29
     */
    public function getAdsList($params=[]){
            $limit         = $params['limit'] ?? 10;
            $advertiser_id = $params['advertiser_id'] ?? "";
            $create_time   = $params['create_time'] ?? "";
            $platform_id   = $params['platform_id'] ?? 0;
            $customDate    = $params['customDate'] ?? 0;
            $admin_id      = $params['adminId'];
            $where         = [];

            if ($create_time) {
                $where[] = ['stat_time', '>=', strtotime($create_time['0'])];
                $where[] = ['stat_time', '<=', strtotime($create_time['1']) + 3600*24-1];
            }


            if(!empty($customDate)){
                switch ($customDate) {
                    case '1':
                        //实时
                        $yesterday = date('Y-m-d', time() - timezone());
                        $midnightYesterday = strtotime($yesterday . ' 00:00:00');
                        $endOfYesterday  = strtotime($yesterday . ' 23:59:59');
                        $where[]  = ['stat_time', '>=', $midnightYesterday];
                        $where[]  = ['stat_time', '<', $endOfYesterday];

                        break;
                    case '2':
                        $yesterday = date('Y-m-d', strtotime('-1 day') - timezone());
                        $midnightYesterday = strtotime($yesterday . ' 00:00:00');
                        $endOfYesterday  = strtotime($yesterday . ' 23:59:59');
                        $where[]  = ['stat_time', '>=', $midnightYesterday];
                        $where[]  = ['stat_time', '<', $endOfYesterday];

                        break;
                    case '3':
                        $yesterday = strtotime(date('Y-m-d', time() - timezone()));
                        $yesterdayEnd = strtotime(date('Y-m-d', strtotime('-6 day') - timezone()));
                        $where[] = ['stat_time', '<', $yesterday];
                        $where[] = ['stat_time', '>=', $yesterdayEnd];

                        break;
                    default:
                        $yesterday = date('Y-m-d', strtotime('-1 day') - timezone());
                        $midnightYesterday = strtotime($yesterday . ' 00:00:00');
                        $endOfYesterday  = strtotime($yesterday . ' 23:59:59');
                        $where[]  = ['stat_time', '>=', $midnightYesterday];
                        $where[]  = ['stat_time', '<', $endOfYesterday];
                }
            }

            if($advertiser_id) {
                $where[] = ['advertiser_id', 'like', '%' . $advertiser_id . '%'];
            }

            if($platform_id) {
                $where[] = ['platform_id','=',$platform_id ];
            }

            $where[] = ['admin_id', '=', $admin_id];


            $admin_name = Db::name('system_admin')->where(['id'=>$admin_id])->value('username');


            $field = 'id,advertiser_id,stat_time_day,shop_id,spend,impressions,platform_id,createtime';

            $list = Db::name('advertising')
                ->field($field)
                ->where($where)
                ->order('stat_time_day desc,spend desc')
                ->paginate($limit);
            $lists = $list->items()??[];


           if($lists){
            //批量获取广告账户名字;
            $advertiser_ids   = array_column($lists,'advertiser_id');
            $advertiser_infos = Db::name('advaccount')->field('id,advertiser_id,info')->whereIn('advertiser_id',$advertiser_ids)->select()->toArray();
            $advertiser_map   = array_column($advertiser_infos,null,'advertiser_id');
            //批量获取店铺名称;
            $shop_ids       = array_column($lists,'shop_id');
            $shop_names     = Db::name('shops')->field('id,name,domain')->whereIn('id',$shop_ids)->select()->toArray();
            $shop_names_map = array_column($shop_names,null,'id');
            $plat_forms     = [
                '1'=>'Facebook',
                '2'=>'Google',
                '3'=>'Tiktok'
            ];
            foreach($lists as $k=>$v){
                $advertiser_map_name          = $advertiser_map[$v['advertiser_id']]??[];
                $lists[$k]['advertiser_name'] = $advertiser_map_name['info']??'';
                $shopNames                    = $shop_names_map[$v['shop_id']]??[];
                $shop_name                    = $shopNames['name']??'';
                $lists[$k]['shop_name']       = $shop_name;
                $lists[$k]['admin_name']      = $admin_name;
                $lists[$k]['create_time']     = $v['createtime'] ? date("Y-m-d H:i:s",$v['createtime']) : '';
            }
            $current_info  =   current($lists);
            $platform_name =   $plat_forms[$current_info['platform_id']]??'';

            $all_list      =   Db::name('advertising')
                ->field($field)
                ->where($where)
                ->select()
                ->toArray();
              $spends_Total =  array_sum(array_column($all_list,'spend'));
            }
            $data = [
                'list'         => $lists,
                'total'        => $list->total(),
                'spends_Total' => isset($spends_Total) && $spends_Total ? getRes($spends_Total) : '0.00',
                'platform_name'=> $platform_name??'',
                'platform_id'  => $platform_id??'',
            ];
            return  $this->jsonMessage(200,true,'获取成功',$data);
    }

    /**
     * @desc   导出广告花费列表
     * @author luckyXiong
     * @date   2025-04-29
     */
    public function export_ads($params=[]){
        $advertiser_id = $params['advertiser_id'] ?? "";
        $create_time   = $params['create_time'] ?? "";
        $platform_id   = $params['platform_id'] ?? 0;
        $customDate    = $params['customDate'] ?? 0;
        $admin_id      = $params['adminId'];
        $where         = [];

        if ($create_time) {
            $where[] = ['stat_time', '>=', strtotime($create_time['0'])];
            $where[] = ['stat_time', '<=', strtotime($create_time['1']) + 3600*24-1];
        }


        if(!empty($customDate)){
            switch ($customDate) {
                case '1':
                    //实时
                    $yesterday = date('Y-m-d', time() - timezone());
                    $midnightYesterday = strtotime($yesterday . ' 00:00:00');
                    $endOfYesterday  = strtotime($yesterday . ' 23:59:59');
                    $where[]  = ['stat_time', '>=', $midnightYesterday];
                    $where[]  = ['stat_time', '<', $endOfYesterday];

                    break;
                case '2':
                    $yesterday = date('Y-m-d', strtotime('-1 day') - timezone());
                    $midnightYesterday = strtotime($yesterday . ' 00:00:00');
                    $endOfYesterday  = strtotime($yesterday . ' 23:59:59');
                    $where[]  = ['stat_time', '>=', $midnightYesterday];
                    $where[]  = ['stat_time', '<', $endOfYesterday];

                    break;
                case '3':
                    $yesterday = strtotime(date('Y-m-d', time() - timezone()));
                    $yesterdayEnd = strtotime(date('Y-m-d', strtotime('-6 day') - timezone()));
                    $where[] = ['stat_time', '<', $yesterday];
                    $where[] = ['stat_time', '>=', $yesterdayEnd];

                    break;
                default:
                    $yesterday = date('Y-m-d', strtotime('-1 day') - timezone());
                    $midnightYesterday = strtotime($yesterday . ' 00:00:00');
                    $endOfYesterday  = strtotime($yesterday . ' 23:59:59');
                    $where[]  = ['stat_time', '>=', $midnightYesterday];
                    $where[]  = ['stat_time', '<', $endOfYesterday];
            }
        }

        if($advertiser_id) {
            $where[] = ['advertiser_id', 'like', '%' . $advertiser_id . '%'];
        }

        if($platform_id) {
            $where[] = ['platform_id','=',$platform_id ];
        }

        $where[] = ['admin_id', '=', $admin_id];


        $admin_name = Db::name('system_admin')->where(['id'=>$admin_id])->value('username');


        $field = 'id,advertiser_id,stat_time_day,shop_id,spend,impressions,platform_id,createtime';

        $lists = Db::name('advertising')
            ->field($field)
            ->where($where)
            ->order('stat_time_day desc,spend desc')
            ->select()
            ->toArray();

        //批量获取广告账户名字;
        $advertiser_ids   = array_column($lists,'advertiser_id');
        $advertiser_infos = Db::name('advaccount')->field('id,advertiser_id,info')->whereIn('advertiser_id',$advertiser_ids)->select()->toArray();
        $advertiser_map   = array_column($advertiser_infos,null,'advertiser_id');
        //批量获取店铺名称;
        $shop_ids       = array_column($lists,'shop_id');
        $shop_names     = Db::name('shops')->field('id,name,domain')->whereIn('id',$shop_ids)->select()->toArray();
        $shop_names_map = array_column($shop_names,null,'id');
        $plat_forms     = [
            '1'=>'Facebook',
            '2'=>'Google',
            '3'=>'Tiktok'
        ];
        foreach($lists as $k=>$v){
            $advertiser_map_name          = $advertiser_map[$v['advertiser_id']]??[];
            $lists[$k]['advertiser_name'] = $advertiser_map_name['info']??'';
            $shopNames                    = $shop_names_map[$v['shop_id']]??[];
            $shop_name                    = $shopNames['name']??'';
            $lists[$k]['shop_name']       = $shop_name;
            $lists[$k]['admin_name']      = $admin_name;
            $lists[$k]['platform_name']   = $plat_forms[$v['platform_id']]??'';
            $lists[$k]['create_time']     = $v['createtime'] ? date("Y-m-d H:i:s",$v['createtime']) : '';

        }
        $list     = self::get_export_path($params);
        $header[] = ['广告平台', 'platform_name'];
        $header[] = ['店铺', 'shop_name'];
        $header[] = ['运营', 'admin_name'];
        $header[] = ['广告账户','advertiser_id'];
        $header[] = ['广告名称', 'advertiser_name'];
        $header[] = ['花费', 'spend'];
        $header[] = ['点击量', 'impressions'];
        $header[] = ['创建时间', 'create_time'];
        $fileName = "export_ads_path";
        return Excel::exportData($lists, $header, $fileName, 'xlsx');
    }







    /**
     * @desc   历史数据列表
     * @author luckyXiong
     * @date   2024-11-06
     */
    private function get_admin_name($admin_ids){
        if(!$admin_ids)  return '';
        //批量获取运营名称;
        $admin_names     = Db::name('system_admin')->whereIn('id',$admin_ids)->column('username');
        return $admin_names ? implode(',',$admin_names) : '';

    }



    /**
     * @desc   历史数据列表
     * @author luckyXiong
     * @date   2024-11-06
     */
    public function editHistoryList($params){
        $admin_id  = $params['admin_id']??'';
        $id        = $params['id']??'';
        if(!$id){
            return  $this->jsonMessage(201,false,'ID不能为空!',null);
        }
        $admin_id = is_array($admin_id) ? implode(',',$admin_id) : $admin_id;
        if($admin_id){
            Db::name('order_page_visits_history')->where('id',$id)->update(
                ['admin_id'=>$admin_id]
            );
        }
        htmlLog(['params'=>$params],OPERATE_LOG . date('Ym') . '/editHistoryList/' . date('Ymd') . '.txt');
        return  $this->jsonMessage(200,true,'操作成功',null);
    }



    /**
     * @desc   历史数据列表
     * @author luckyXiong
     * @date   2024-11-11
     */
     public function exportHistoryList($params){
         $list     = self::get_export_history($params);
         $header[] = ['店铺', 'shop_name'];
         $header[] = ['页面类型', 'page_type'];
         $header[] = ['页面路径', 'page_path'];
         $header[] = ['访客','total_visitors'];
         $header[] = ['访问', 'total_sessions'];
         $header[] = ['添加到购物车', 'total_carts'];
         $header[] = ['进入结算流程', 'total_checkouts'];
         $header[] = ['访问持续时间', 'avg_duration'];
         $header[] = ['跳出率', 'total_bounce_rate'];
         $header[] = ['转化率', 'total_conversion'];
         $header[] = ['成功准换的访问数', 'total_orders_placed'];
         $header[] = ['页面浏览量', 'total_pageviews'];
         $header[] = ['运营', 'admin_name'];
         $header[] = ['创建时间', 'create_time'];
         $fileName = "Report_history_visits";
         return Excel::exportData($list, $header, $fileName, 'xlsx');
     }


    /**
     * @desc   获取员工角色
     * @author luckyXiong
     * @date   2024-11-11
     */
     public function getRoleId($params=[]){
        $admin_id = $params['admin_id']??"";
        $group_id = Db::name('system_admin')->where('id',$admin_id)->value('group_ids');
        return  $group_id ? : '';
     }


    /**
     * @desc   运营新增投放链接
     * @author luckyXiong
     * @date   2025-04-23
     */
     public function add_page_path($params=[]){
            $admin_id  = $params['admin_id']??'';
            $page_type = $params['page_type']??'';
            $page_path = $params['page_path']??'';
            $spu       = $params['spu']??'';
            //获取当前运营对应的店铺
            $shop_id   = Db::name('order_page_shop_admin')->where(['admin_id'=>$admin_id])->value('shop_id');
            if($page_type=='Product' && $shop_id){
                $spu   = Db::name('order_page_visits_product')->where(['shop_id'=>$shop_id,'page_path'=>$page_path])->value('spu');
            }

            if($page_type!='Product' && !$spu){
                return $this->jsonMessage(201,true,'非产品页spu不能为空','');
            }


            //数据入库;
            $insert_info = [
                'shop_id'    =>$shop_id??'',
                'admin_id'   =>$admin_id,
                'page_type'  =>$page_type,
                'page_path'  =>$page_path,
                'spu'        =>$spu,
                'create_time'=>date("Y-m-d H:i:s", time()),
            ];

            Db::name('order_page_visits_admin')->insert($insert_info);
            return $this->jsonMessage(200,true,'操作成功','');
     }


    /**
     * @desc   运营投放链接列表
     * @author luckyXiong
     * @date   2025-04-25
     */
     public function get_page_path($params=[]){
         $limit                = getIsset($params,'limit',10);
         $shop_id              = getIsset($params,'shop_id');
         $create_time          = getIsset($params,'create_time');
         $page_type            = $params['page_type']??'';
         $spu                  = $params['spu']??'';
         $admin_id             = $params['admin_id']??'';
         $group_id             = $params['group_id']??'';
         $where                = $order = [];

         if ($create_time) {
             $where[]  = ['create_time', '>=', date('Y-m-d H:i:s',strtotime($create_time[0]))];
             $where[]  = ['create_time', '<=', date('Y-m-d H:i:s',strtotime($create_time[1]) + 3600*24-1)];
         }

         if($shop_id){
             $where[] = ['shop_id','=',$shop_id];
         }

         if($page_type){
             $where[] = ['page_type','=',$page_type];
         }

         if($spu){
             $where[] = ['spu','=',$spu];
         }

         if($group_id==11){
             $where[] = ['admin_id', '=',$admin_id];
         }

         $field = '*';
         $res   = Db::name('order_page_visits_admin')
             ->field($field)
             ->where($where)
             ->order($order)
             ->paginate($limit);
         $list = $res->items()?:[];

         if($list) {
             //批量获取店铺名称;
             $shop_ids = array_column($list, 'shop_id');
             $shop_names = Db::name('shops')->whereIn('id', $shop_ids)->select()->toArray();
             $shop_names_map = array_column($shop_names, null, 'id');
             foreach ($list as $k => $v) {
                 $shopNames = $shop_names_map[$v['shop_id']] ?? [];
                 $list[$k]['shop_name'] = $shopNames['name'] ?? '';
                 $user_id = $v['admin_id'] ?? '';
                 $adminNames = self::get_admin_name($user_id);
                 $list[$k]['admin_name'] = $adminNames;
                 $domain  =  $shopNames['domain'] ?? '';
                 if($v['page_path']){
                     $list[$k]['product_url'] = 'https://'.$domain.$v['page_path'];
                 }else{
                     $list[$k]['product_url'] = '';
                 }
             }
         }


         $data = [
             'list'   => $list,
             'total'  => $res->total(),
         ];
         return  $this->jsonMessage(200,true,'获取成功',$data);
     }


    /**
     * @desc   运营投放链接列表
     * @author luckyXiong
     * @date   2025-04-25
     */
    public function get_page_path_new($params=[]){
        $limit                = getIsset($params,'limit',10);
        $create_time          = getIsset($params,'create_time');
        $page_type            = $params['page_type']??'';
        $spu                  = $params['spu']??'';
        $admin_id             = $params['adminId']??'';
        $where                = [];

        if ($create_time) {
            $where[]  = ['create_time', '>=', date('Y-m-d H:i:s',strtotime($create_time[0]))];
            $where[]  = ['create_time', '<=', date('Y-m-d H:i:s',strtotime($create_time[1]) + 3600*24-1)];
        }


        if($page_type){
            $where[] = ['page_type','=',$page_type];
        }

        if($spu){
            $where[] = ['spu','=',$spu];
        }

        $where[] = ['admin_id', '=',$admin_id];


        $field = '*';
        $res   = Db::name('order_page_visits_admin')
            ->field($field)
            ->where($where)
            ->order('create_time desc')
            ->paginate($limit);
        $list = $res->items()?:[];

        if($list) {
            //批量获取店铺名称;
            $shop_ids = array_column($list, 'shop_id');
            $shop_names = Db::name('shops')->whereIn('id', $shop_ids)->select()->toArray();
            $shop_names_map = array_column($shop_names, null, 'id');
            foreach ($list as $k => $v) {
                $shopNames = $shop_names_map[$v['shop_id']] ?? [];
                $list[$k]['shop_name'] = $shopNames['name'] ?? '';
                $domain  =  $shopNames['domain'] ?? '';
                $user_id = $v['admin_id'] ?? '';
                $adminNames = self::get_admin_name($user_id);
                $list[$k]['admin_name']  = $adminNames;
                if($v['page_path']){
                    $list[$k]['product_url'] = 'https://'.$domain.$v['page_path'];
                }else{
                    $list[$k]['product_url'] = '';
                }
            }
        }


        $data = [
            'list'   => $list,
            'total'  => $res->total(),
        ];
        return  $this->jsonMessage(200,true,'获取成功',$data);
    }



    /**
     * @desc   获取员工角色
     * @author luckyXiong
     * @date   2024-11-11
     */
    public function editRoleId($params=[]){
        $admin_id  = $params['admin_id']??'';
        $id        = $params['shop_id']??'';
        $admin_id = is_array($admin_id) && $admin_id  ? implode(',',$admin_id) : $admin_id;

        if($admin_id){
            Db::name('shops')->where('id',$id)->update(
                ['admin_id'=>$admin_id]
            );
        }else{
            Db::name('shops')->where('id',$id)->update(
                ['admin_id'=>'']
            );
        }
        htmlLog(['params'=>$params],OPERATE_LOG . date('Ym') . '/editRoleIdProduct/' . date('Ymd') . '.txt');
        return  $this->jsonMessage(200,true,'操作成功',null);
    }


    /**
     * @desc   登陆页面访问情况
     * @author luckyXiong
     * @date   2025-03-13
     */
    public function getPathList($params=[]){
        $limit                = getIsset($params,'limit',10);
        $shop_id              = getIsset($params,'shop_id');
        $create_time          = getIsset($params,'create_time');
        $total_visitors       = $params['total_visitors']??'';
        $total_sessions       = $params['total_sessions']??'';
        $total_carts          = $params['total_carts']??'';
        $total_checkouts      = $params['total_checkouts']??'';
        $where                = $order = [];

        if ($create_time) {
            $where[]  = ['create_time', '>=', date('Y-m-d H:i:s',strtotime($create_time[0]))];
            $where[]  = ['create_time', '<=', date('Y-m-d H:i:s',strtotime($create_time[1]) + 3600*24-1)];
        }

        if($shop_id){
            $where[] = ['shop_id','=',$shop_id];
        }

        if($total_visitors){
            $order['total_visitors'] = $total_visitors;
        }

        if($total_sessions){
            $order['total_sessions'] = $total_sessions;
        }

        if($total_carts){
            $order['total_carts'] = $total_carts;
        }


        if($total_checkouts){
            $order['total_checkouts'] = $total_checkouts;
        }


        if(!$total_visitors && !$total_sessions &&!$total_carts &&!$total_checkouts ){
            $order['create_time'] = 'desc';
        }

        $field = '*';
        $res   = Db::name('order_path_visits')
            ->field($field)
            ->where($where)
            ->order($order)
            ->paginate($limit);
        $list = $res->items()?:[];

        if($list){
            //批量获取店铺名称;
            $shop_ids       = array_column($list,'shop_id');
            $shop_names     = Db::name('shops')->whereIn('id',$shop_ids)->select()->toArray();
            $shop_names_map = array_column($shop_names,null,'id');
            foreach($list as $k=>$v){
                $shopNames             = $shop_names_map[$v['shop_id']]??[];
                $list[$k]['shop_name'] = $shopNames['name']??'';
            }
        }
        return [
            'list'   => $list,
            'total'  => $res->total(),
        ];
    }










}